package ATM.controller;

import ATM.Service.ATMService;
import ATM.domain.Account;

import java.util.Scanner;

public class ATMController {
    private Scanner sc = new Scanner(System.in);

    public void show(Account account) {
        if(account.getGender().equals("男")){
            System.out.println("恭喜您,"+account.getName()+"先生"+"登录成功,您的卡号是:"+account.getCardId());
        }else {
            System.out.println("恭喜您,"+account.getName()+"女士"+"登录成功,您的卡号是:"+account.getCardId());
        }

        while (true) {
            System.out.println("==================ATM==================");
            System.out.println("1.查询账号");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.密码修改");
            System.out.println("6.退出");
            System.out.println("7.注销当前账户");
            System.out.println("请选择");
            String str = sc.next();
            switch (str){
                case "1":
                    findLoginAccount(account);
                    break;
                case "2":
                    setMoneyByAccount(account);
                    break;
                case "3":
                    withdrawMoneyByAccount(account);
                    break;
                case "4":
                    transferMoneyByAccount(account);
                    break;
                case "5":
                    boolean flag = updatePassword(account);
                    if(flag){
                        return;
                    }
                    break;
                case "6":
                    //退出
                    if(account.getGender().equals("男")){
                        System.out.println(account.getName()+"先生"+"谢谢使用,退出成功");
                    }else {
                        System.out.println(account.getName()+"女士"+"谢谢使用,退出成功");
                    }
                    return;
                case "7":
                    boolean flag2 = deleteAccount(account);
                    if(flag2){
                        return;
                    }
                    break;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }
    }

    private boolean deleteAccount(Account loginAcc) {
        while (true){
            System.out.println("是否确定要注销，确定y，否定n");
            String str = sc.next();
            if("n".equalsIgnoreCase(str)){
                System.out.println("取消成功");
                return false;
            } else if ("y".equalsIgnoreCase(str)) {
                if(loginAcc.getRemain()>0){
                    System.out.println("对不起，您的账户中有存钱金额，不允许注销");
                    return false;
                }

                ATMService atmService = new ATMService();
                boolean flag = atmService.deleteAccount(loginAcc);

                if (flag) {
                    loginAcc = null;
                    System.out.println("账号注销成功！");
                    return true;
                }else {
                    System.out.println("账号注销失败");
                    return false;
                }
            }
        }
    }

    private boolean updatePassword(Account loginAcc) {
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入密码");
            String password = sc.next();

            if(!password.equals(loginAcc.getPassword())){
                System.out.println("您当前输的密码不正确，还剩"+i+"次机会");
            }else {
                while (true) {
                    System.out.println("请输入修改后的密码");
                    String newPassword1 = sc.next();
                    System.out.println("请再次输入密码");
                    String newPassword2 = sc.next();
                    if(newPassword1.equals(newPassword2)){
                        loginAcc.setPassword(newPassword1);
                        return true;
                    }else{
                        System.out.println("您输入的密码不一致，请重新输入");
                    }
                }

            }
        }
        return false;
    }

    private void transferMoneyByAccount(Account loginAcc) {
        ATMService atmService = new ATMService();
        int num = atmService.AccountNum();
        if(num < 2){
            System.out.println("没有其他账号，无法转账");
            return;
        }

        if(loginAcc.getRemain()==0){
            System.out.println("没钱转啥账，穷鬼！");
            return;
        }

        for (int i = 0; i < 3; i++) {
            System.out.println("请输入要转的卡号");
            String cardId = sc.next();

            Account account = atmService.findAccountByCardId(cardId);
            if(account == null){
                System.out.println("未能发现该账号，请确认后再次输入，还剩"+(2-i)+"次工作");
            }else {
                String username = account.getName();
                String uName = username.replace(username.substring(0,1),"*");
                System.out.println("请补全当前账号姓氏"+uName);
                String str = sc.next();
                if(!str.equals(username.substring(0,1))){
                    System.out.println("对不起，您的姓氏有问题，转账失败！");
                    return;
                }
                while (true){
                    System.out.println("请输入要转账的金额");
                    double money = sc.nextDouble();
                    if (money>loginAcc.getRemain()){
                        //  - 如果转账金额超过余额，给出提示“对不起，余额不足，转账失败！”
                        System.out.println("对不起，余额不足，转账失败！");
                        return;
                    }else {
                        //- 7、如果对方卡号存在、姓氏匹配、余额足够，就完成真正的转账操作
                        //  - 获取当前自己账户的余额，减去转账金额，就可以得到自己账户新的余额，
                        //自己的账号减钱
                        loginAcc.setRemain(loginAcc.getRemain()-money);
                        //对方的账号加钱
                        account.setRemain(account.getRemain()+money);
                        System.out.println("转账成功,本次转账:"+money+"元,余额:"+loginAcc.getRemain());
                        return;
                    }
                }
            }


        }


    }

    private void withdrawMoneyByAccount(Account loginAcc) {
        if(loginAcc.getRemain()<100){
            System.out.println("金额不足一百元，不能取钱");
            return;
        }
        while (true){
            System.out.println("请输入要取款的金额：");
            double money = sc.nextDouble();

            if(loginAcc.getLimitMoney()<money){
                System.out.println("您输入的金额大于取款限额，请重新输入");
            }else {
                if(loginAcc.getRemain()<money){
                    System.out.println("余额不足，请重新输入：");
                }else {
                    loginAcc.setRemain(loginAcc.getRemain()-money);
                    System.out.println(loginAcc.getName()+"取款成功，本次取款"+
                            money+"元，余额："+loginAcc.getRemain());
                    return;
                }
            }
        }
    }

    private void setMoneyByAccount(Account loginAcc) {
        while (true){
            System.out.println("请输入要存入的金额");
            double money = sc.nextDouble();
            if(money<0){
                System.out.println("您输入的金额有误，请重新输入：");
            }else {
                loginAcc.setRemain(loginAcc.getRemain()+money);
                System.out.println(loginAcc.getName()+"恭喜您，存款成功，本次存款"+
                        money+"元，账户金额："+loginAcc.getRemain());
                return;
            }
        }
    }

    private void findLoginAccount(Account loginAcc) {
        System.out.println("--------------------");
        System.out.println("卡号："+loginAcc.getCardId());
        System.out.println("用户名："+loginAcc.getName());
        System.out.println("性别："+loginAcc.getGender());
        System.out.println("余额："+loginAcc.getRemain());
        System.out.println("限额："+loginAcc.getLimitMoney());
        System.out.println("--------------------");
    }






}
